package SocialMediaSentiment;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import jxl.write.WriteException;

public class NaiveBayes2 {

	public static void main(String[] args) throws IOException, WriteException {

		while (true) {

			HashMap<String, String> toprintZero = new HashMap<String, String>();
			HashMap<String, String> toprintPos = new HashMap<String, String>();
			HashMap<String, String> toprintNeg = new HashMap<String, String>();
			int totalZero = 0;
			int totalPos = 0;
			int totalNeg = 0;
			int totalZeroWrong = 0;
			int totalPosWrong = 0;
			int totalNegWrong = 0;
			int neutopos = 0;
			int neutoneg = 0;
			int neutoneu = 0;
			int postopos = 0;
			int postoneg = 0;
			int postoneu = 0;
			int negtopos = 0;
			int negtoneg = 0;
			int negtoneu = 0;

			float[] sentimentChance = getSentimentChance();
			float sentimentChancePos = sentimentChance[0];
			float sentimentChanceNeg = sentimentChance[1];
			float sentimentChanceNeut = sentimentChance[2];
			HashMap<String, Float> wordsCountAll = getWordsCount("smallCorrectedCounterTrainSetAllSignificant10plus.xls");
			HashMap<String, Float> wordsCountPos = getWordsCount("smallCorrectedCounterTrainSetSignificantBiggerThanZero.xls");
			HashMap<String, Float> wordsCountNeg = getWordsCount("smallCorrectedCounterTrainSetSignificantLessThanZero.xls");
			HashMap<String, Float> wordsCountNeut = getWordsCount("smallCorrectedCounterTrainSetSignificantZero.xls");
			float totalWordsPos = getTotalCount(wordsCountPos);
			float totalWordsNeg = getTotalCount(wordsCountNeg);
			float totalWordsNeut = getTotalCount(wordsCountNeut);

			HashMap<Integer, String[]> tweetsWithSentiment = ReadExcel.read(
					"smallCorrectedTweets.xls", 0, 1);
			Iterator<Integer> k = (Iterator<Integer>) tweetsWithSentiment
					.keySet().iterator();
			while (k.hasNext()) {
				int index = k.next();
				String[] data = tweetsWithSentiment.get(index);
				String tweet = data[0];
				int sentiment = Integer.parseInt(data[1].replace(" ", ""));
				float posPos = getTweetPos(tweet, wordsCountPos, totalWordsPos,
						sentimentChancePos, wordsCountAll);
				float negPos = getTweetPos(tweet, wordsCountNeg, totalWordsNeg,
						sentimentChanceNeg, wordsCountAll);
				float neutPos = getTweetPos(tweet, wordsCountNeut,
						totalWordsNeut, sentimentChanceNeut, wordsCountAll);
				int classi = getClassification(posPos, negPos, neutPos);
				if (sentiment == 0) {
					if (classi > 0)
						neutopos++;
					if (classi < 0)
						neutoneg++;
					if (classi == 0)
						neutoneu++;
					if (classi != 0) {
						toprintZero.put(tweet, "" + classi);
						totalZeroWrong++;
					}
					totalZero++;
				} else if (sentiment > 0) {

					if (classi > 0)
						postopos++;
					if (classi < 0)
						postoneg++;
					if (classi == 0)
						postoneu++;
					if (classi <= 0)

					{
						toprintPos.put(tweet, "" + classi);
						totalPosWrong++;
					}
					totalPos++;
				} else if (sentiment < 0) {

					if (classi > 0)
						negtopos++;
					if (classi < 0)
						negtoneg++;
					if (classi == 0)
						negtoneu++;
					if (classi >= 0)

					{
						toprintNeg.put(tweet, "" + classi);
						totalNegWrong++;
					}
					totalNeg++;
				}
				/*
				 * System.out.println(tweet); System.out.println(sentiment + "v"
				 * + classi + "--" + posPos + "||" + negPos + "||" + neutPos);
				 * 
				 * toprint.put(tweet + " " + sentiment, "" + posPos + "||" +
				 * negPos + "||" + neutPos);
				 */
			}
			writeExcel.write("wrongBayesClassNeg.xls", toprintNeg);
			writeExcel.write("wrongBayesClassPos.xls", toprintPos);
			writeExcel.write("wrongBayesClassZero.xls", toprintZero);
			System.out
					.println("||||||||||||||||||||||||||||||||||||||||||||||||");
			System.out.println("total:\t" + totalZero + "\t\t" + totalPos
					+ "\t\t" + totalNeg);
			System.out.println("wrong:\t" + totalZeroWrong + "\t\t"
					+ totalPosWrong + "\t\t" + totalNegWrong);
			System.out.println("\tNeutral\t\tPositive\tNegative");
			System.out.println("neu:\t" + neutoneu + "\t\t" + postoneu + "\t\t"
					+ negtoneu);
			System.out.println("pos:\t" + neutopos + "\t\t" + postopos + "\t\t"
					+ negtopos);
			System.out.println("neg:\t" + neutoneg + "\t\t" + postoneg + "\t\t"
					+ negtoneg);
			System.out
					.println("||||||||||||||||||||||||||||||||||||||||||||||||");
			updateCounters
					.updateCounterFile(
							"smallCorrectedCounterTrainSetSignificantBiggerThanZero.xls",
							"wrongBayesClassPos.xls",
							createSignificantCounter
									.getSignificantWords("smallCorrectedCounterTrainSetAllSignificant10plus.xls"));
			updateCounters
					.updateCounterFile(
							"smallCorrectedCounterTrainSetSignificantLessThanZero.xls",
							"wrongBayesClassNeg.xls",
							createSignificantCounter
									.getSignificantWords("smallCorrectedCounterTrainSetAllSignificant10plus.xls"));
			// updateCounters.updateCounterFile("smallCorrectedCounterTrainSetSignificantZero.xls",
			// "wrongBayesClassZero.xls",
			// createSignificantCounter.getSignificantWords("smallCorrectedCounterTrainSetAllSignificant10plus.xls"));
		}
	}

	public static int getClassification(float pos, float neg, float neut) {
		int classification = 1;
		if (neg > pos && neg > neut) {
			classification = -1;
		} else if (neut > pos) {
			classification = 0;
		}
		return classification;

	}

	public static float getTweetPos(String tweet,
			HashMap<String, Float> wordsCount, float totalWords,
			float sentimentChance, HashMap<String, Float> wordsCountAll) {
		StringTokenizer tokenizer = new StringTokenizer(tweet);
		Float chance;
		float tweetPos = sentimentChance;
		// float words = tokenizer.countTokens();
		// tweetPos = 0;
		while (tokenizer.hasMoreTokens()) {
			String word = tokenizer.nextToken();
			if (wordsCountAll.keySet().contains(word)) {
				try {
					chance = (2 * wordsCount.get(word)) / totalWords;
				} catch (NullPointerException e) {
					chance = 1 / totalWords;
				}

				// System.out.println(chance + "  " + tweetPos + "   " + word);
				tweetPos = tweetPos * chance * 1000;
			}
		}
		return tweetPos;
	}

	public static HashMap<String, Float> getWordsCount(String file)
			throws IOException {
		HashMap<String, Float> wordsCount = new HashMap<String, Float>();
		HashMap<Integer, String[]> IndexWordsCounter = ReadExcel.read(file, 0,
				1);
		Iterator<Integer> k = (Iterator<Integer>) IndexWordsCounter.keySet()
				.iterator();
		while (k.hasNext()) {
			int index = k.next();
			String[] WordsCounter = IndexWordsCounter.get(index);
			String word = WordsCounter[0];
			float count = Float.parseFloat(WordsCounter[1]);
			wordsCount.put(word, (count + 1));
		}
		return wordsCount;
	}

	public static float getTotalCount(HashMap<String, Float> wordsCount)
			throws IOException {
		float total = 0;
		Iterator<String> k = (Iterator<String>) wordsCount.keySet().iterator();
		total += wordsCount.keySet().size();
		while (k.hasNext()) {
			String word = k.next();
			float count = wordsCount.get(word);
			total += count;
		}
		return total;
	}

	public static float[] getSentimentChance() throws IOException {
		float[] sentimentChance = new float[3];
		float pos = 0;
		float neg = 0;
		float neut = 0;
		float total = 0;

		HashMap<Integer, String[]> IndexTweets = ReadExcel.read(
				"CorrectedTweets.xls", 0, 1);
		Iterator<Integer> k = (Iterator<Integer>) IndexTweets.keySet()
				.iterator();
		while (k.hasNext()) {
			int index = k.next();
			String[] TweetSentiment = IndexTweets.get(index);
			int sentiment = Integer
					.parseInt(TweetSentiment[1].replace(" ", ""));
			if (sentiment > 0)
				pos++;
			else if (sentiment < 0)
				neg++;
			else
				neut++;
			total++;
		}
		sentimentChance[0] = pos / total;
		sentimentChance[1] = neg / total;
		sentimentChance[2] = neut / total;
		return sentimentChance;

	}

}
